package son.vpoint.dinokids;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import org.anddev.andengine.util.MathUtils;

import son.vpoint.dinokids.until.LoadImageAssets;
import son.vpoint.dinokids.until.MathFactory;
import son.vpoint.dinokids.view.GameView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GameActivity extends Activity{
	private int status = -1;
	private ImageView[] arrImageItems;
	public int DEVICE_WIDTH;
	public int DEVICE_HEIGHT;
	private ArrayList<String> arrSo;
	private ArrayList<String> arrItems, arrPuts;
	private ArrayList<Integer> arrIndexTrue;
	private int itemsRemain;
	private String[] strSo;
	private String srtHienthimanchoi="";
	public static int x, x1;
	public static int y, y1;
	private Integer[] arrX;
	private Integer[] arrY;

	private LoadImageAssets mLoadImageAssets;
	private LayoutParams lp;
	private RelativeLayout rlbackhome;
	private AbsoluteLayout mode1_layout;
	private TextView txtHienthimanchoi;
	private TextView txtScore;
	private TextView txtTodayBestscore;
	private Button btnback;
	private OperatorSelected mOperatorSelected;
	private GameActivity mGameActivity;
	public GameView mGameView;
	public MathFactory mMathFactory;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.maingame);
	
		mOperatorSelected = new OperatorSelected();
		mGameActivity = new GameActivity();
		mMathFactory = new MathFactory();
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		Typeface mFontHienthimanchoi = Typeface.createFromAsset(getAssets(),
				"fonts/vnipikachu.TTF");

		txtHienthimanchoi = (TextView) findViewById(R.id.txthienthimanchoi);
		txtHienthimanchoi.setTypeface(mFontHienthimanchoi);
		txtHienthimanchoi.setTextSize(20);
		txtHienthimanchoi.setText(""+this.getTextBoard(mOperatorSelected.mSelecOperator, srtHienthimanchoi));
		
		txtScore =(TextView)findViewById(R.id.txtMyScore);
		
		txtTodayBestscore =(TextView)findViewById(R.id.txtTodayBest);
		btnback = (Button)findViewById(R.id.btnback);
		btnback.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(v.getId()==R.id.btnback)
				{
					AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this);
					builder.setTitle("Exit!")
							.setMessage("Are you sure you want to exit?")
							.setCancelable(false)
							.setPositiveButton("OK", new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									Intent i = new Intent(GameActivity.this, OperatorSelected.class);
									startActivity(i);
									finish();
									
								}

						



							})
							.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									dialog.cancel();
									
								}
							});
					builder.create().show();
				
				}
			}
		});
		DEVICE_WIDTH = mDisplayMetrics.widthPixels;
		DEVICE_HEIGHT = mDisplayMetrics.heightPixels;
		
		x = DEVICE_WIDTH;
		y = DEVICE_HEIGHT;
		x1 = x / 10;
		y1 = 3 * y / 4;
		arrX = new Integer[] { x1, 2 * x1 - x1 / 6, 3 * x1 - x1 / 3,
				4 * x1 - x1 / 2, 4 * x1+x1/2, 5 * x1 + x1 / 2,
				6 * x1 +x1/2, 7 * x1 + x1 / 2, 8 * x1+x1/2 };
		arrY = new Integer[] { y1-30, y1-30, y1-30, y1-30, y1-30, y1-30, y1-30, y1-30, y1-30 };

		
		strSo = new String[] { "so1", "so2", "so3", "so4", "so5", "so6", "so7",
				"so8", "so9" };
		
		arrSo = new ArrayList<String>();
		
		Collections.addAll(arrSo, strSo);
	
		mGameView = new GameView(GameActivity.this);
		mode1_layout = (AbsoluteLayout) findViewById(R.id.Almaingame);
		mode1_layout.addView(mGameView);
		arrImageItems = new ImageView[9];
	
		arrImageItems[0] = (ImageView) findViewById(R.id.imgSo1);
		arrImageItems[0].setBackgroundResource(R.drawable.so1);
		arrImageItems[1] = (ImageView) findViewById(R.id.imgSo2);
		arrImageItems[1].setBackgroundResource(R.drawable.so2);
		arrImageItems[2] = (ImageView) findViewById(R.id.imgSo3);
		arrImageItems[2].setBackgroundResource(R.drawable.so3);
		arrImageItems[3] = (ImageView) findViewById(R.id.imgSo4);
		arrImageItems[3].setBackgroundResource(R.drawable.so4);
		arrImageItems[4] = (ImageView) findViewById(R.id.imgSo5);
		arrImageItems[4].setBackgroundResource(R.drawable.so5);
		arrImageItems[5] = (ImageView) findViewById(R.id.imgSo6);
		arrImageItems[5].setBackgroundResource(R.drawable.so6);
		arrImageItems[6] = (ImageView) findViewById(R.id.imgSo7);
		arrImageItems[6].setBackgroundResource(R.drawable.so7);
		arrImageItems[7] = (ImageView) findViewById(R.id.imgSo8);
		arrImageItems[7].setBackgroundResource(R.drawable.so8);
		arrImageItems[8] = (ImageView) findViewById(R.id.imgSo9);
		arrImageItems[8].setBackgroundResource(R.drawable.so9);
	
		mLoadImageAssets = new LoadImageAssets();
		arrItems = new ArrayList<String>();
	
		arrIndexTrue = new ArrayList<Integer>();
		rlbackhome = (RelativeLayout) findViewById(R.id.rlbackhome);
		
		try {
			arrItems = mLoadImageAssets.CreatArrayImage(GameActivity.this,
					"items");
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		for (int i = 0; i < 9; i++) {

			mGameActivity.setLayoutParamView(lp, arrImageItems[i], arrX[i], arrY[i]);
			

		}
//		mGameActivity.setLayoutParamView(lp, mGameView, 100, 100);
		mGameActivity.setLayoutParamView(lp, txtHienthimanchoi, x/4, 0);
		mGameActivity.setLayoutParamView(lp, rlbackhome, x/9, y/9);
		mGameActivity.setLayoutParamView(lp, txtScore, 7*x/9, y/15);
		mGameActivity.setLayoutParamView(lp, txtTodayBestscore, 7*x/9, 3*y/15);
	
		// Move cac ImgView
		arrImageItems[0].setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				status = 0;
				if (event.getAction() == MotionEvent.ACTION_UP) {
					status = -1;
				}
				return false;
			}
		});

		arrImageItems[1].setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				status = 1;
				if (event.getAction() == MotionEvent.ACTION_UP) {
					status = -1;
				}
				return false;
			}
		});
		arrImageItems[2].setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				status = 2;
				if (event.getAction() == MotionEvent.ACTION_UP) {
					status = -1;
				}
				return false;
			}
		});
		arrImageItems[3].setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				status = 3;
				if (event.getAction() == MotionEvent.ACTION_UP) {
					status = -1;
				}
				return false;
			}
		});
		arrImageItems[4].setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				status = 4;
				if (event.getAction() == MotionEvent.ACTION_UP) {
					status = -1;
				}
				return false;
			}
		});
		arrImageItems[5].setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				status = 5;
				if (event.getAction() == MotionEvent.ACTION_UP) {
					status = -1;
				}
				return false;
			}
		});
		arrImageItems[6].setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				status = 6;
				if (event.getAction() == MotionEvent.ACTION_UP) {
					status = -1;
				}
				return false;
			}
		});
		arrImageItems[7].setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				status = 7;
				if (event.getAction() == MotionEvent.ACTION_UP) {
					status = -1;
				}
				return false;
			}
		});
		arrImageItems[8].setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				status = 8;
				if (event.getAction() == MotionEvent.ACTION_UP) {
					status = -1;
				}
				return false;
			}
		});
		mode1_layout.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {

				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					v.bringToFront();
				}
				if (status != -1) {

					lp = new AbsoluteLayout.LayoutParams(
							android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
							android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
							(int) event.getX() - arrImageItems[0].getWidth()
									/ 2, (int) event.getY()
									- arrImageItems[0].getHeight() / 2);
					arrImageItems[status].setLayoutParams(lp);

				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					int a = (int)event.getX();
					int b = (int)event.getY();
					if (status != -1) {
						if(mMathFactory.getDifficultLevel() ==0){
							
						if(mMathFactory.getParam1() == -1){
							
							if(a >150 && a < 250 && b >320 && b<450 ){
								mGameActivity.setLayoutParamView(lp, arrImageItems[status], 150, 350);
							}
							else{
								// Chay ve cho cu
							mGameActivity.setLayoutParamView(lp, arrImageItems[status], arrX[status], arrY[status]);

								status = -1;
							}
							
						}
					}
//						if ((int) event.getX() > 2 * x / 3
//								&& (int) event.getY() > y / 2) {
//							if (CheckTrue(status) == true) {
//								
//								Log.i("Checkstatus:", "true");
//
//								itemsRemain--;
//
//								arrImageItems[status].setVisibility(View.GONE);
//
//								if (itemsRemain == 0) {
//
//									arrIndexTrue.clear();
//									for (int j = 0; j < arrIndexTrue.size(); j++) {
//										arrImageItems[arrIndexTrue.get(j)]
//												.setVisibility(View.VISIBLE);
//									}
//								}
//							} else {
//
//							}
//
//						}
						
				
					}
				}

				return true;
			}
		});

	}

	public boolean CheckTrue(Integer st) {
		boolean test = false;
		for (int i = 0; i < arrIndexTrue.size(); i++) {
			if (st == arrIndexTrue.get(i)) {
				test = true;
			}
		}
		return test;

	}
	public String getTextBoard(int i,String str){
		switch (i) {
		case 1:
			str ="Addition";
			break;
		case 2:
			str ="Subtraction";
			break;
		case 3:
			str ="Multiplication";
			break;
		case 4:
			str ="Division";
			break;
		case 5:
			str ="Mix";
			break;

		default: 
			break;
		}
		return str;
	}
	public void setLayoutParamView(android.view.ViewGroup.LayoutParams lp,
			View mTextView, int width, int heigth) {
		lp = new AbsoluteLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT, width, heigth);
	
		mTextView.setLayoutParams(lp);

	}
    public int getWidth(){
    	return x;
    }
    public int getHeight(){
    	return y;
    }
    public void checkAdd(){
    	switch (mMathFactory.getDifficultLevel()) {
		case 0:
			
			break;

		default:
			break;
		}
    }
    public void checkSub(){
    	
    }
    public void checkMulti(){
    	
    }
    public void checkDivis(){
    	
    }
    public void checkMix(){
    	
    }
}
